<?php

/**
 * MVM_MALL 网上商店系统  后台短信发送
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-02-27 $
 * $Id: sendsms.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/
header("Content-type: text/html;charset=utf-8");
if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if($action=='add') {
	if($_POST){
		if(!$cmd){
			$cmd = "send";//操作命令;
		}
		if ($send_type==1) {
			$goods_grant = (int)$goods_grant;
			$sql  = $goods_grant==1 ? "SELECT member_tel1,member_class FROM `{$tablepre}member_table` WHERE member_class='$goods_grant'":"SELECT member_tel1,member_class FROM `{$tablepre}member_table`";
			$result = $db->query($sql);
			while ($rt=$db->fetch_array($result)) {
				$rt['member_tel1']!='' && $receivers.=$rt['member_tel1'].';';
			}
		}
		$loginname = md5($mm_sms_system_id);	//短信帐号
		$pass =	md5($mm_sms_system_pass);	//密码
		$message = trim($message);		//url编码后的短信内容
		$settimer = urlencode(trim($settimer));	//定时发送时间
		$sender_name = urlencode("$mm_mall_name");
		$code = "";				//业务代码
		$subid = "";				//扩展号
		$sender = "$mm_sms_tel";		//发送号码
		$allowforward = "";			//转发支持
		$recordid_client = $m_check_id;		//客户短信标识
		$receivers = $receivers;		//接收号码
		$http = "$mm_sms_system_url";
		$file = $http."?loginname=".$loginname."&pass=".$pass."&code=".$code."&subid=".$subid."&sender=".$sender."&receivers=".$receivers."&settimer=".$settimer."&message=".urlencode($message)."&cmd=".$cmd."&allowforward=".$allowforward."&sender_name=".$sender_name."&output=&recordid_client=".$recordid_client."&charset=utf8";
		$parser = xml_parser_create();		//对像的创建
		xml_set_element_handler($parser,"startElement","endElement");
		xml_set_character_data_handler($parser, "characterData");
		$filehandler = fopen($file, "r");
		while ($data = fread($filehandler, 5000)) { //读取对像
			xml_parse($parser, $data, feof($filehandler));
		}
		if ($cmd == "send")	{
			$str = sms_output_sta($GLOBALS['RetCode']);
			echo "<script language='javascript'>alert('$str');</script>";
			exit();
		}else {
			$str = '您还有'.$GLOBALS['SmsRemain'].'短信可发送';
			echo "<script language='javascript'>alert('$str');window.close();</script>";
			exit();
		}
	}
	else {
		$arr = $cache->get_cache('grade');
		$arr[0]='All';
		$grant_menu   = drop_menu($arr,'goods_grant');
		require_once template('sendsms');
		footer();
	}
} else {
	admin_msg('pass_worng');
}

function startElement($parser_instance, $element_name, $attrs) {	//对像处理函数
	switch($element_name) {
		case "SUCCESS":
			break;
		case "FAIL":
			break;
		case "RETCODE":
			break;
		case "SMSREMAIN":
			break;
		case "ERRPHONES":
			break;
	}
}
function characterData($parser_instance, $xml_data) {
	global $value;
	$value = $xml_data;
}

function endElement($parser_instance, $element_name) {
	switch($element_name) {
		case "SUCCESS":
			break;
		case "FAIL":
			break;
		case "RETCODE":
			global $RetCode;
			$RetCode=$GLOBALS['value'];
			break;
		case "SMSREMAIN":
			global $SmsRemain;
			$SmsRemain=$GLOBALS['value'];
			break;
		case "ERRPHONES":
			break;
	}
}
